#!/bin/bash
set -e
set -u
#
# Run a simple backup
#   then restore it.
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-susan-fd
#shellcheck source=../environment.in
. ./environment

#shellcheck source=../scripts/functions
. "${rscripts}"/functions
"${rscripts}"/cleanup
"${rscripts}"/setup



# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data "$@"

start_test

start_susan

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out /dev/null
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
label volume=TestVolume001 storage=File pool=Full
label volume=TestVolume002 storage=File pool=Incremental
label volume=TestVolume003 storage=File pool=VirtualFull
run job=$JobName level=Full yes
wait
messages
list jobs
@exec "sh -c 'touch ${tmp}/data/*.c'"
run job=$JobName level=Incremental yes
wait
messages
list jobs
run job=$JobName level=VirtualFull yes
wait
messages
list jobs
@# make sure we really restore the virtualfull
purge volume=TestVolume001
delete volume=TestVolume001
purge volume=TestVolume002
delete volume=TestVolume002
status director
status client
status storage=File
quit
END_OF_DATA

run_bconsole "$tmp/bconcmds"

cat <<END_OF_DATA >"$tmp/bconcmds2"
@#
@# now do a restore
@#
@$out $tmp/log2.out
wait
restore client=susan-fd fileset=SelfTest where=$tmp/susan-restores select all done
yes
wait
messages
quit
END_OF_DATA
                                                                                
run_bconsole "$tmp/bconcmds2"

check_for_zombie_jobs storage=File
stop_susan

check_two_logs
check_restore_diff "${BackupDirectory}"
end_test
